package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class TransferPlanEndTimeHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    private static final String TIME = "2029-12-31 23:59:59";
    private static final Logger LOGGER=LoggerFactory.getLogger(TransferPlanEndTimeHandler.class);

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {

        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(data.getSceneInstId());

        kiteProdInstList.forEach(KiteProdInst -> {
            Product product = ProductCache.get(KiteProdInst.getProdId());
            if (KiteStringUtils.isEqual(KeyConsts.PROD_FUNC_TYPE_101, product.getProdFuncType())) {

                KiteAttr kiteAttr = KiteProdInst.getAttrMap().get(LocalKeyConsts.MAIN_BILLING_MODE);
                if (!Objects.isNull(kiteAttr)) {
                    if (KiteStringUtils.isEqual(LocalKeyConsts.BILLING_CHARTER_MODE, kiteAttr.getValue())) {
                        attr.setValue(TIME);
                    }
                }
            }
        });
    }

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        String period = inst.getAttrValue(LocalKeyConsts.TRANSFER_APPOINTMENT_CYCLE); // 预约周期
        if (KiteStringUtils.isNotEmpty(period)) {
            //todo 转定会错误拦截，应做不同流程判断
            //this.checkTime(period, value);
        }

        String beginAttrValue = inst.getAttrValue(LocalKeyConsts.TRANSFER_APPOINTMENT_START);

        if (KiteStringUtils.isEmpty(beginAttrValue)) {
            return;
        }

        int num = DateFormatUtils.compareDate(beginAttrValue, value, DateFormatUtils.DATE_TIME_FORMAT);

        if (num > 0) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("预约开始时间不能大于预约结束时间，请核实后再操作！");
        }

        int points = DateFormatUtils.getPoinsByStrs(beginAttrValue, value, DateFormatUtils.DATE_TIME_FORMAT);

        if (points < 60) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("租用时长需大于60分钟！");
        }

        // int day = DateFormatUtils.getDayByStrs(beginAttrValue, endAttrValue, DateFormatUtils.DATE_TIME_FORMAT);
        // if (day > 5) {
        // ErrorConsts.BUSI_COMMON_ERROR.throwOut("预约开始时间与预约结束时间相隔不能大于5个工作日！");
        // }
    }

    /**
     * 周期约束时间
     *
     * @param inst
     * @param value
     */
    public void checkTime(String period, String value) {
        Date beginTime = null;
        Date endTime = null;
        Date nowTime = null;
        SimpleDateFormat df = new SimpleDateFormat(DateFormatUtils.DATE_TIME_FORMAT);
        if (KiteStringUtils.isEqual(period, LocalKeyConsts.ATTRVALUE_571)) {
            // 周
            try {
                beginTime = df.parse(DateFormatUtils.getCurrentMonday()); // 周一
                endTime = df.parse(DateFormatUtils.getPreviousSunday()); // 周日
                nowTime = df.parse(value); // 选中时间
            }
            catch (ParseException e) {
                // TODO 自动生成的 catch 块
                LOGGER.error(e.getMessage());
            }
            if (nowTime.getTime() < beginTime.getTime() || nowTime.getTime() > endTime.getTime()) {
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("当前选中的时间不在本周的时间范围内，请确认后再操作！");
            }
        }
        else if (KiteStringUtils.isEqual(period, LocalKeyConsts.ATTRVALUE_572)) {
            // 月
            try {
                beginTime = df.parse(DateFormatUtils.getMinMonthDate()); // 月初
                endTime = df.parse(DateFormatUtils.getMaxMonthDate()); // 月末
                nowTime = df.parse(value); // 选中时间
            }
            catch (ParseException e) {
                // TODO 自动生成的 catch 块
                LOGGER.error(e.getMessage());
            }
            if (nowTime.getTime() < beginTime.getTime() || nowTime.getTime() > endTime.getTime()) {
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("当前选中的时间不在本月的时间范围内，请确认后再操作！");
            }
        }
        else {
            // 其他
        }
    }
}
